package com.apes.fn.scm.advancePayment;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.util.MapUtil;
import com.apes.scm.account.account.model.AccountingMethod;
import com.apes.scm.account.account.model.InventoryMode;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.account.tax.service.TaxService;
import com.apes.scm.invoice.model.Invoice;
import com.apes.scm.invoice.model.InvoiceItem;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * 订金 e 保养服务
 * @Author wuq
 * @Date 2021-8-24
 */
@Service
public class AdvanceInsuranceDiscountsService extends DomainService {

    @Autowired
    private PartyRoleRepository partyRoleRepository;
    @Autowired
    private ProductRepository productRepository;

    private Dept getDept(PartyRole company) {
        return SpringManager.getBean(DeptRepository.class)
                .findAll(JpaDsl.toCriteriaByEq("company", company, "innerTag", true))
                .stream().findFirst().orElseThrow(() -> new RuntimeException("没有找到内定标记部门，请先定义！公司：：" + company.getId()));
    }

    public void process(AdvancePayment advancePayment){
        Invoice invoice = new Invoice();

        invoice.setCompany(partyRoleRepository.findById("1003").get());
        invoice.setDept(getDept(invoice.getCompany()));

        // 取收款方式的客户
        AdvancePaymentMethod advancePaymentMethod = advancePayment.getPaymentMethodSet().iterator().next();
        invoice.setPartner(advancePaymentMethod.getCustomer().getParty().getPartyRoleByType("Supplier"));   // 供应商

        invoice.setSource(advancePayment.getId());
        invoice.setSourceBusinessType(AdvancePayment.class.getSimpleName());
        invoice.setOrigin(advancePayment.getId());
        invoice.setOriginBusinessType(AdvancePayment.class.getSimpleName());

        invoice.setInventoryMode(InventoryMode.IM_OWN);             //设置库存模式：自有
        invoice.setAccountingMethod(AccountingMethod.AM_PURCHASE);  // 核算方式：经销

        // 明细数据
        Set<InvoiceItem> invoiceItems = new HashSet();
        InvoiceItem invoiceItem = new InvoiceItem();
        Product product = productRepository.findOne("0002935068");    //获取挂账特殊商品:(e保养+)项目
        invoiceItem.setProduct(product);

        // 获取税率
        Tax tax = SpringManager.getBean(TaxService.class).getPurchaseTax(invoice.getCompany(), invoice.getPartner(), product);
        invoiceItem.setTax(tax);

        invoiceItem.setPriceUnit(advancePayment.getInsuranceDiscountsAmt());
        invoiceItem.setQty(1);
        invoiceItem.setUom(product.getUom());

        invoiceItem.setSourceItemId(advancePayment.getPaymentItems().iterator().next().getId() );   // 明细编码
        invoiceItem.setOriginItemId(advancePayment.getPaymentItems().iterator().next().getId() );   // 明细编码
        invoiceItem.setPriceSubtotal(advancePayment.getInsuranceDiscountsAmt());    // 商品金额
        invoiceItems.add(invoiceItem);

        invoice.setInvoiceItems(invoiceItems);
        invoke("invoice.create", MapUtil.mapped(invoice));
    }

}
